package com.lpf;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.io.UnsupportedEncodingException;

import static com.lpf.util.CodeUtils.decodeUTF8Str;

public class Md5Python {

    private static final String pythonFuncFilePath = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\apps\\md5\\main\\run.py";

    private static final String encryption = "encryption";

    public static void main(String[] args) throws UnsupportedEncodingException {

        long startTime = System.currentTimeMillis();

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(pythonFuncFilePath);

        PyFunction pyf = interpreter.get(encryption, PyFunction.class);

        PyObject[] pyObjects = new PyObject[2];
        pyObjects[0] = Py.newStringUTF8("你在干嘛");
        pyObjects[1] = Py.newStringUTF8("小写");

        PyObject pyObject = pyf.__call__(pyObjects);
        JSONObject jsonObject = JSONUtil.parseObj(decodeUTF8Str(pyObject.toString()));
        System.out.println("小写加密结果：" + jsonObject);

        pyObjects = new PyObject[2];
        pyObjects[0] = Py.newStringUTF8("你在干嘛");
        pyObjects[1] = Py.newStringUTF8("大写");

        pyObject = pyf.__call__(pyObjects);
        jsonObject = JSONUtil.parseObj(decodeUTF8Str(pyObject.toString()));
        System.out.println("大写加密结果：" + jsonObject);

        interpreter.cleanup();

        long endTime = System.currentTimeMillis();
        System.out.println("执行时间：" + (endTime - startTime));
    }

}
